package com.banksoft.cps.util;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version 1.0
 */
public class DateUtil {
    private static Logger logger = Logger.getLogger(DateUtil.class);
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    //把2014-01-01  格式的时间转换成long类型
    public static long getTime(String time) {
        if(StringUtil.isNullOrEmpty(time)) {
            return 0;
        }
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            return 0;
        }
        return date.getTime();
    }

    //把2014-01-01 12:12:12格式的时间转换成long类型
    public static long getTime2(String time) throws ParseException {
        Date date = format2.parse(time);
        return date.getTime();
    }

    //获取指定年月日的开始时间
    public static long getZdDayBeginTime(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    //获取指定年月日的结束时间
    public static long getZdDayEndTime(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime().getTime();
    }

    //获取指定年月的开始时间
    public static long getZdYearMonthBeginTime(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    //获取指定年月的结束时间
    public static long getZdYearMonthEndTime(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
//        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.DAY_OF_MONTH, getZdYearMonthDays(year,month));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime().getTime();
    }

    //获取指定年月的天数
    public static int getZdYearMonthDays(int year, int month){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DATE, 1);
        c.roll(Calendar.DATE, -1);
        return c.get(Calendar.DATE);
    }


    //获取指定年的开始时间
    public static long getZDYearBeginTime(int year) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    //获取指定年的结束时间
    public static long getZDYearEndTime(int year) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, 11);
        c.set(Calendar.DAY_OF_MONTH, 31);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime().getTime();
    }

    //获取昨天开始时间
    public static long yesterdayBegTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    //获取昨天结束时间
    public static long yesterdayEndTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime().getTime();
    }

    //获取当天开始时间
    public static long currentDayBeginTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND,0);
        return c.getTime().getTime();
    }

    //获取当天结束时间
    public static long currentDayEndTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime().getTime();
    }

    //获取当天指定时分秒的时间
    public static long currentDayHMSTime(int h, int m, int s) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, h);
        c.set(Calendar.MINUTE, m);
        c.set(Calendar.SECOND, s);
        return c.getTime().getTime();
    }

    public static long getZDDayHMSTime(long time, String hhmmss) {
        try {
            String[] array = hhmmss.split(":");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(time));
            c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(array[0]));
            c.set(Calendar.MINUTE, Integer.parseInt(array[1]));
            c.set(Calendar.SECOND, Integer.parseInt(array[2]));
            return c.getTime().getTime();
        } catch (Exception ex) {
            logger.error("指定还款时间格式错误" + hhmmss, ex);
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            return c.getTime().getTime();
        }

    }

    //获取当月开始时间毫秒数
    public static long currentMonthBeginTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    //获取当月结束时间毫秒数
    public static long currentMonthEndTime() {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, 0);
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); //获取当月最大天数
        ca.set(Calendar.HOUR_OF_DAY, 23);
        ca.set(Calendar.MINUTE, 59);
        ca.set(Calendar.SECOND, 59);
        return ca.getTime().getTime();
    }

    //获取当年开始时间毫秒数
    public static long currentYearBeginTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    //获取当年结束时间毫秒数
    public static long currentYearEndTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, 11);
        c.set(Calendar.DAY_OF_MONTH, 31);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        System.out.println(format.format(c.getTime()));
        return c.getTime().getTime();
    }

    //根据时间毫秒数获取年
    public static int getYear(long time) {
        Date date = new Date(time);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    //根据时间毫秒数获取月
    public static int getMonth(long time) {
        Date date = new Date(time);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }

    //根据时间毫秒数获取日
    public static int getDay(long time) {
        Date date = new Date(time);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    public static String getDateStringByFormat(long time, String format) {
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.format(new Date(time));
    }

    /**
     * 得到当前时间之后n分钟的时间
     * @param currTime
     * @param minute
     * @return
     */
    public static long getAfterMinuteTime(long currTime,int minute){
        return currTime + minute * 60l * 1000l;
    }
    /**
     * 得到当前时间之前n分钟的时间
     * @param currTime
     * @param minute
     * @return
     */
    public static long getBeforeMinuteTime(long currTime,int minute){
        return currTime - minute * 60l * 1000l;
    }
    /**
     * 得到当前时间之后n天之后的时间
     *
     * @param currTime
     * @param dayCount
     * @return
     */
    public static long getAfterDayTime(long currTime, int dayCount) {
        return currTime + dayCount * 24l * 60l * 60l * 1000l;
    }

    public static long getBeforeDayTime(long currTime, int dayCount) {
        return currTime - dayCount * 24l * 60l * 60l * 1000l;
    }

    public static long getAfterMonthTime(long currTime, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currTime));
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime().getTime();
    }
    //计算月份，1号的当前时间
    public static long getbeforeMonthTime(long l, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(l);
        calendar.set(Calendar.DATE,1);
        calendar.add(Calendar.MONTH, i);
        return calendar.getTimeInMillis();
    }

    public static long getAfterWeekTime(long currTime, int week) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currTime));
        calendar.add(Calendar.WEEK_OF_MONTH, week);
        return calendar.getTime().getTime();
    }

    //获取 yyyyMMddHHmmss 格式的时间
    public static String getyMdHmsateDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date());
    }

    //获取 yyyyMMdd 格式的时间
    public static String getyMdDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(new Date());
    }

    /**
     * 判断时间是否是今天的时间
     *
     * @param time
     * @return
     */
    public static boolean isInToday(long time) {
        long begin = currentDayBeginTime();
        long end = currentDayEndTime();
        return (time >= begin && time <= end);
    }

    /**
     * 获取当前月第一天
     * @return
     */
    public static long getCurrentMonthFirstDay(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.getTime();
        return truncateTime(c.getTime()).getTime();
    }

    public static long getMonthFirstDay(long time){
        Calendar c = Calendar.getInstance();
        c.setTime( new Date(time));
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);
        c.getTime();
        return truncateTime(c.getTime()).getTime();
    }

    /**
     * 获取当前月最后一天
     * @return
     */
    public static long getNextMonthFirstDay(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH,1);
        return truncateTime(c.getTime()).getTime();
    }

    /**
     * 获取本周第一天
     * @return
     */
    public static long getCurrentWeekFirstDay(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return truncateTime(cal.getTime()).getTime();
    }
    /**
     * 日期截断
     * @param date
     * @return
     */
    public static Date truncateTime(Date date){
        Date result = null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String s = sdf.format(date);
            result =  sdf.parse(s);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 日期加减
     * @param date
     * @param formate
     * @param addDay
     * @return
     */
    public static String getDateStrAddDay(String date,String formate,int addDay){
        String result  = null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat(formate);
            Date day = sdf.parse(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(day);
            cal.add(Calendar.DATE,addDay);
            Date dest = cal.getTime();
            result = sdf.format(dest);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }

    public static Date  getDateAddDay(Date date,int addDay){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,addDay);
        Date dest = cal.getTime();
        return dest;
    }
    public static void main(String[] args){
        getCurrentMonthFirstDay();
        getNextMonthFirstDay();
        System.out.println(DateUtil.getDateStringByFormat(new Date().getTime(),"yyyyMMddHHmmsss"));
    }

}
